﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class index : System.Web.UI.Page
{
    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
    SqlConnection sqlcon;
    //SqlCommand sqlcom;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i =Convert.ToInt32(DateTime.Now.Year) ; i >=2011 ; i--)
            {
                nian.Items.Add(i.ToString());
            }
            for (int i = 1; i <= 12; i++)
            {
                yue.Items.Add(i.ToString());
            }
            nian.SelectedValue = DateTime.Now.Year.ToString();
            yue.SelectedValue = DateTime.Now.Month.ToString();
            Label_tiaojian.Text = "请选择汇总条件并点击汇总按钮！";

        //g_bind();
        }
        GridView1.Attributes.Add("bordercolor", "#bbddff");
        Label_tishi.Visible = false;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
           
            //当有编辑列时，避免出错，要加的RowState判断 
            //if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            //{
            //    ((LinkButton)e.Row.Cells[17].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除这条记录吗?')");
            //}
            SqlDataSource s = (SqlDataSource)e.Row.FindControl("SqlDataSource_bzmx");
            string bzmx = "SELECT     cailiaoguanli.banzu, cailiaomingxi.duanshuliang AS zongshu, cailiaomingxi.beizhu FROM         cailiaomingxi INNER JOIN cailiaoguanli ON cailiaomingxi.c_id = cailiaoguanli.c_id";
             bzmx+=" WHERE     (cailiaomingxi.duanshuliang <> 0) AND (cailiaomingxi.c_id IN (SELECT     c_id FROM          cailiaoguanli AS cailiaoguanli_1 WHERE      ( 1= 1)";
        if (DropDownList_lei.SelectedValue != "请选择")
        {
            bzmx += " AND (lei = '" + DropDownList_lei.SelectedValue + "')";
        }
        if (nian.SelectedValue != "请选择")
        {
            bzmx += " AND (nian = '" + nian.SelectedValue + "')";
        }
        if (yue.SelectedValue != "请选择")
        {
            bzmx += " AND (yue = '" + yue.SelectedValue + "')";
        }
        if (DropDownList_sscj.SelectedValue != "请选择")
        {
            bzmx += " AND (chejian = '" + DropDownList_sscj.SelectedValue + "')";
        }
        if (DropDownList_zrbz.SelectedValue != "请选择")
        {
            bzmx += " AND (banzu = '" + DropDownList_zrbz.SelectedValue + "')";
        }
        if (DropDownList_shenpi.SelectedValue == "段已审批未受理")
        {
            bzmx += " AND (zt =4)";
        }
        else if (DropDownList_shenpi.SelectedValue == "材料科已受理")
        {
            bzmx += " AND (zt =5)";
        }
        else if (DropDownList_shenpi.SelectedValue == "全部段已审批")
        {
            bzmx += " AND (zt>=4)";
        }

        bzmx += " AND (cailiaomingxi.leibie1 = '" + e.Row.Cells[2].Text + "') AND (cailiaomingxi.bianhao = '" + e.Row.Cells[1].Text.Replace("&nbsp;", "") + "') AND (cailiaomingxi.mingcheng = '" + e.Row.Cells[4].Text + "') AND (cailiaomingxi.guige = '" + e.Row.Cells[5].Text.Replace("&nbsp;", "") + "') AND (cailiaomingxi.danwei = '" + e.Row.Cells[6].Text.Replace("&nbsp;", "") + "') GROUP BY c_id)) order BY cailiaoguanli.banzu, cailiaomingxi.duanshuliang, cailiaomingxi.beizhu";
        s.SelectCommand = bzmx;
        GridView gr = (GridView)e.Row.FindControl("GridView_bzmx");
        gr.Attributes.Add("bordercolor", "#bbddff");
        gr.DataBind();
        int heji = 0;
        for (int i = 0; i < gr.Rows.Count; i++)
        {
            heji += Convert.ToInt32(gr.Rows[i].Cells[1].Text);
        }
        if (heji != Convert.ToInt32(e.Row.Cells[7].Text))
        {
            e.Row.ForeColor = System.Drawing.Color.Red;
            e.Row.Font.Bold = true;
            Label_tishi.Visible = true;
            
        }

        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1 + GridView1.PageIndex * GridView1.PageSize;
            e.Row.Cells[0].Text = id.ToString();
            //e.Row.Cells[5].Text =e.Row.Cells[5].Text.Replace("&amp;#215;", "×").Replace("&amp;quot;", "");
        }
    }
    protected void DropDownList_sscj_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList_zrbz.Items.Clear();
        DropDownList_zrbz.Items.Add(new ListItem("请选择"));
        DropDownList_zrbz.DataBind();

    }

    public void g_bind()
    {
        Label_tiaojian.Text = "当前选择汇总条件:";
        
        string sqlstr = "SELECT     leibie1, leibie2, bianhao, mingcheng, guige,danwei, sum(duanshuliang) AS zongshu FROM         cailiaomingxi WHERE  duanshuliang<>0 and    (c_id IN (";
        sqlstr+="select c_id FROM cailiaoguanli WHERE 1=1" ;
        if (DropDownList_lei.SelectedValue != "请选择")
        {
            sqlstr += " AND (lei = '" + DropDownList_lei.SelectedValue + "')";
            Label_tiaojian.Text += "类别：" + DropDownList_lei.SelectedValue + "；  ";
        }
        if (nian.SelectedValue != "请选择")
        {
            sqlstr += " AND (nian = '" + nian.SelectedValue + "')";
            Label_tiaojian.Text += nian.SelectedValue + "年；  ";
        }
        if (yue.SelectedValue != "请选择")
        {
            sqlstr += " AND (yue = '" + yue.SelectedValue + "')";
            Label_tiaojian.Text += yue.SelectedValue + "月；  ";
        }
        if (DropDownList_sscj.SelectedValue != "请选择")
        {
            sqlstr += " AND (chejian = '" + DropDownList_sscj.SelectedValue + "')";
            Label_tiaojian.Text += "车间：" + DropDownList_sscj.SelectedValue + "；  ";
        }
        if (DropDownList_zrbz.SelectedValue != "请选择")
        {
            sqlstr += " AND (banzu = '" + DropDownList_zrbz.SelectedValue + "')";
            Label_tiaojian.Text += "班组：" + DropDownList_zrbz.SelectedValue + "；  ";
        }
        if (DropDownList_shenpi.SelectedValue == "段已审批未受理")
        {
            sqlstr += " AND (zt =4)";
            Label_tiaojian.Text += "审批情况：" + DropDownList_shenpi.SelectedValue + "；  ";
        }
        else if (DropDownList_shenpi.SelectedValue == "材料科已受理")
        {
            sqlstr += " AND (zt =5)";
            Label_tiaojian.Text += "审批情况：" + DropDownList_shenpi.SelectedValue + "；  ";
        }
        else if (DropDownList_shenpi.SelectedValue == "全部段已审批")
        {
            sqlstr += " AND (zt>=4)";
            Label_tiaojian.Text += "审批情况：" + DropDownList_shenpi.SelectedValue + "；  ";
        }
        sqlstr += " GROUP BY c_id)) GROUP BY mingcheng, guige, leibie1, leibie2, bianhao,danwei ORDER BY bianhao,leibie1, leibie2,mingcheng,guige ";
        if (Label_tiaojian.Text == "当前选择汇总条件:")
        {
            Label_tiaojian.Text = "当前选择汇总条件:无";
        }
        //Response.Write(sqlstr);
        //Response.End();
        //sqlcon = new SqlConnection(strCon);
        //SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        //DataSet myds = new DataSet();
        ////sqlcon.Open();
        //myda.Fill(myds, "ganbutiancheng");
        //GridView1.DataSource = myds;
        SqlDataSource1.ConnectionString = strCon;
        SqlDataSource1.SelectCommand = sqlstr;
        GridView1.DataSourceID = SqlDataSource1.ID;
        //GridView1.EnableSortingAndPagingCallbacks = false;
        GridView1.PagerStyle.Font.Size = 10;
        //GridView1.PagerSettings.Position = PagerPosition.TopAndBottom;
        //GridView1.PagerStyle.Font.Bold = true;
        //GridView1.DataBind();
        if (DropDownList_shenpi.SelectedValue == "材料科已受理")
        {
            try
            {
                //Response.Write(sqlstr);
                //Response.Write(sqlstr.Replace("*", "c_id"));
                //Response.End();
                string sqlstr1 = "SELECT SUM(danjia * duanshuliang) AS zongjia FROM  cailiaomingxi WHERE     (c_id IN (" + sqlstr.Replace("SELECT     leibie1, leibie2, bianhao, mingcheng, guige,danwei, sum(duanshuliang) AS zongshu FROM         cailiaomingxi WHERE  duanshuliang<>0 and    (c_id IN (", "").Replace(")) GROUP BY mingcheng, guige, leibie1, leibie2, bianhao,danwei ORDER BY leibie1, leibie2,mingcheng,guige ", "") + ")) AND (duanshuliang <> 0)";
                //Response.Write(sqlstr1);
                sqlcon = new SqlConnection(strCon);
                SqlDataAdapter myda1 = new SqlDataAdapter(sqlstr1, sqlcon);
                DataSet myds1 = new DataSet();
                //sqlcon.Open();
                myda1.Fill(myds1, "siteman1");
                if (myds1.Tables[0].Rows.Count != 0)
                {
                    label_zongjia.Visible = true;
                    label_zongjia.Text = "当前选择条件下计划单的总价为：" + myds1.Tables[0].Rows[0]["zongjia"].ToString() + "元";
                    if (label_zongjia.Text == "当前选择条件下计划单的总价为：元")
                    {
                        label_zongjia.Visible = false;
                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }
        }
        else
        {
            label_zongjia.Visible = false;
        }
        string biaoti = nian.SelectedItem + "年" + yue.SelectedItem + "月 " + DropDownList_sscj.SelectedItem + " " + DropDownList_zrbz.SelectedItem + " " + DropDownList_shenpi.SelectedValue + "材料汇总（"+DropDownList_lei.SelectedItem+"）";
        Label_biaoti.Text = biaoti.Replace("请选择年", "").Replace("请选择月", "").Replace("请选择", "").Replace("（）", "");
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        g_bind();
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        DropDownList_lei.SelectedValue = "请选择";
        nian.SelectedValue = "请选择";
        yue.SelectedValue = "请选择";
        DropDownList_sscj.SelectedValue = "请选择";
        DropDownList_zrbz.SelectedValue = "请选择";
        DropDownList_shenpi.SelectedValue = "全部段已审批";
        nian.SelectedValue = DateTime.Now.Year.ToString();
        yue.SelectedValue = DateTime.Now.Month.ToString();
        g_bind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        g_bind();
        ImageButton1.Focus();
   }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridViewRow btrow = GridView1.BottomPagerRow;
        Label lb1 = new Label();
        lb1.Text = "当前页码(" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + ")";
        if (btrow != null)
        {
            btrow.Cells[0].Controls.Add(lb1);        
        }

    }
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {

    }
}
